<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="doc">

  <xsl:variable name="title_color" select="'#225566'"/>

  <xsl:template match="doc:chapter">
    <block color="{$title_color}" id="{@id}" keep-with-next.within-page="always">
      <xsl:variable name="heading" select="count(ancestor::doc:chapter)"/>
      <xsl:choose>
        <xsl:when test="$heading = 0">
          <xsl:attribute name="font-size">12pt</xsl:attribute>
          <xsl:attribute name="margin-top">18pt</xsl:attribute>
          <xsl:attribute name="margin-bottom">6pt</xsl:attribute>
        </xsl:when>
        <xsl:when test="$heading = 1">
          <xsl:attribute name="font-size">11pt</xsl:attribute>
          <xsl:attribute name="margin-top">12pt</xsl:attribute>
          <xsl:attribute name="margin-bottom">5pt</xsl:attribute>
        </xsl:when>
        <xsl:when test="$heading = 2">
          <xsl:attribute name="font-size">10pt</xsl:attribute>
          <xsl:attribute name="margin-top">11pt</xsl:attribute>
          <xsl:attribute name="margin-bottom">4pt</xsl:attribute>
        </xsl:when>
        <xsl:when test="$heading = 3">
          <xsl:attribute name="font-size">9pt</xsl:attribute>
          <xsl:attribute name="margin-top">10pt</xsl:attribute>
          <xsl:attribute name="margin-bottom">4pt</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="font-size">9pt</xsl:attribute>
          <xsl:attribute name="margin-top">10pt</xsl:attribute>
          <xsl:attribute name="margin-bottom">4pt</xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:number level="multiple" count="doc:chapter" format="1.1"/>
      <xsl:text> </xsl:text>
      <xsl:apply-templates select="doc:title/node()"/>
    </block>
    <xsl:apply-templates select="*[name(.)!='title']"/>
  </xsl:template>

  <xsl:template match="doc:paragraph">
    <block margin-left="6mm" margin-top="4pt" margin-right="4mm" margin-bottom="4pt">
      <xsl:apply-templates select="node()"/>
    </block>
  </xsl:template>

  <xsl:template match="doc:subject">
    <block margin-left="6mm" margin-top="4pt" margin-right="4mm" margin-bottom="2pt">
      <xsl:if test="position() = 1">
        <xsl:attribute name="keep-with-next.within-page">always</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="doc:title/node()"/>
    </block>
    <block margin-left="8mm" margin-top="2pt" margin-right="4mm" margin-bottom="4pt"
      keep-together.within-page="always" font-size="9pt">
      <xsl:apply-templates select="doc:content/node()"/>
    </block>
  </xsl:template>

</xsl:stylesheet>
